﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using Microsoft.Practices.CompositeWeb.Web.UI;

namespace Locator.PersonalLocations.Views
{
    public partial class AddPersonalLocations : UserControl, IAddPersonalLocationsView
    {
        private AddPersonalLocationsPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.presenter.OnViewInitialized();
            }

            this.presenter.OnViewLoaded();
        }

        [CreateNew]
        public AddPersonalLocationsPresenter Presenter
        {
            get { return this.presenter; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this.presenter = value;
                this.presenter.View = this;
            }
        }

        public string StreetAddress
        {
            get { return this.TextBoxStreetAddress.Text; }
        }

        public string City
        {
            get { return this.TextBoxCity.Text; }
        }

        public string PostalCode
        {
            get { return this.TextBoxPostalCode.Text; }
        }

        public string Province
        {
            get { return this.ProvinceDropDownList.SelectedValue; }
        }

        public decimal? Latitude
        {
            get
            {
                decimal? result;
                if (!String.IsNullOrEmpty(this.LatitudeTextBox.Text))
                {
                    result = Convert.ToDecimal(this.LatitudeTextBox.Text);
                }
                else
                {
                    result = null;
                }

                return result;
            }

            set { this.LatitudeTextBox.Text = value.ToString(); }
        }

        public decimal? Longitude
        {
            get
            {
                decimal? result;
                if (!String.IsNullOrEmpty(this.LongitudeTextBox.Text))
                {
                    result = Convert.ToDecimal(this.LongitudeTextBox.Text);
                }
                else
                {
                    result = null;
                }

                return result;
            }

            set { this.LongitudeTextBox.Text = value.ToString(); }
        }

        public string Description
        {
            get { return this.DescriptionTextBox.Text; }
        }

        protected void AddPersonalLocationButton_Click(object sender, EventArgs e)
        {
            this.presenter.OnAddPersonalLocationClicked();
        }

        protected void ButtonSearchByPostalCode_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchByPostalCodeClicked();
        }

        protected void ButtonSearchByAddress_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchByAddressClicked();
        }

        public void SetProvinces(List<string> provinces)
        {
            this.ProvincesDataSource.DataSource = provinces;
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	



        public void Reset()
        {
            this.DescriptionTextBox.Text = String.Empty;
            this.LatitudeTextBox.Text = String.Empty;
            this.LongitudeTextBox.Text = String.Empty;
        }

        public string Result
        {
            get { return this.ResultLabel.Text; }
            set
            {
                this.ResultLabel.Text = value;
                if (!String.IsNullOrEmpty(value))
                {
                    this.ResultLabel.Visible = true;
                }
                else
                {
                    this.ResultLabel.Visible = false;
                }
            }
        }

        public override void Dispose()
        {
            if (this.presenter != null)
            {
                this.presenter.Dispose();
            }

            base.Dispose();
        }
    }
}

